package com.dxy.df.core.shiro;

import com.dxy.df.constants.ConstNumbers;
import com.dxy.df.constants.ConstStrings;
import com.dxy.df.utils.WebUtil;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.subject.Subject;
import org.apache.shiro.web.filter.authc.AuthenticatingFilter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.util.StringUtils;

import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.security.Principal;
import java.util.Objects;

/**
 * API过滤器
 *
 * @date 2017-05-20 13:00
 */
public class ApiFilter extends AuthenticatingFilter {

    private static final Logger LOGGER = LoggerFactory.getLogger(ApiFilter.class);

    private static final String CASE_DOMAIN;

    static {
        if (Objects.equals("local", System.getProperty(ConstStrings.SPRING_PROFILES_ACTIVE))) {
            CASE_DOMAIN = "http://caselocal.sim.dxy.net";
        } else if (Objects.equals("test", System.getProperty(ConstStrings.SPRING_PROFILES_ACTIVE))) {
            CASE_DOMAIN = "https://case.sim.dxy.net";
        } else if (Objects.equals("uat", System.getProperty(ConstStrings.SPRING_PROFILES_ACTIVE))) {
            CASE_DOMAIN = "https://caseuat.sim.dxy.cn";
        } else {
            CASE_DOMAIN = "https://case.sim.dxy.cn";
        }
    }

    @Override
    protected AuthenticationToken createToken(ServletRequest request, ServletResponse response) throws Exception {
        //获取请求token
        String ticket = WebUtil.getRequestTicket((HttpServletRequest) request);
        if (StringUtils.isEmpty(ticket)) {
            return null;
        }
        return new LoginToken(ticket);
    }

    @Override
    protected boolean isAccessAllowed(ServletRequest request, ServletResponse response, Object mappedValue) {
        return false;
    }

    @Override
    protected boolean onAccessDenied(ServletRequest request, ServletResponse response) throws Exception {

        HttpServletRequest httpServletRequest = (HttpServletRequest) request;
        HttpServletResponse httpServletResponse = (HttpServletResponse) response;
        Principal t = httpServletRequest.getUserPrincipal();

        if (Objects.equals("local", System.getProperty(ConstStrings.SPRING_PROFILES_ACTIVE))) {
            String testDebugDxyUserName = httpServletRequest.getHeader(ConstStrings.TEST_DEBUG_DXYUSERNAME);
            if (null != testDebugDxyUserName && !StringUtils.isEmpty(testDebugDxyUserName)) {
                return true;
            }
        }


        if (null != t) {
            Subject currentUser = SecurityUtils.getSubject();
            if (currentUser.isAuthenticated()) {
                currentUser.getSession().setTimeout(ConstNumbers.SERVER_SESSION_TIMEOUT);
            }
            return true;
        } else {
            return WebUtil.loginFailResponse(response);
        }

    }

    @Override
    protected boolean onLoginFailure(AuthenticationToken token, AuthenticationException e,
                                     ServletRequest request, ServletResponse response) {
        return WebUtil.loginFailResponse(response);
    }



}
